// SingleServDlg.cpp : implementation file
//

#include "stdafx.h"
#include "service control.h"
#include "SingleServDlg.h"

#ifdef _DEBUG
//#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSingleServDlg dialog


CSingleServDlg::CSingleServDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSingleServDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSingleServDlg)
	m_csExitCode = _T("");
	m_csFullName = _T("");
	m_csProcessId = _T("");
	m_csShortName = _T("");
	m_csState = _T("");
	m_csStopMsg = _T("");
	//}}AFX_DATA_INIT
}


void CSingleServDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSingleServDlg)
	DDX_Text(pDX, IDC_SEXITCODE, m_csExitCode);
	DDX_Text(pDX, IDC_SFULLNAME, m_csFullName);
	DDX_Text(pDX, IDC_SPID, m_csProcessId);
	DDX_Text(pDX, IDC_SSHORTNAME, m_csShortName);
	DDX_Text(pDX, IDC_SSSTATE, m_csState);
	DDX_Text(pDX, IDC_SSSTOPMSG, m_csStopMsg);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSingleServDlg, CDialog)
	//{{AFX_MSG_MAP(CSingleServDlg)
	ON_BN_CLICKED(IDC_BTNSSSTART, OnBtnssstart)
	ON_BN_CLICKED(IDC_BTNSSSTOP, OnBtnssstop)
	ON_BN_CLICKED(IDC_SSBTNKILL, OnSsbtnkill)
	ON_BN_CLICKED(IDC_SSBTNSETSTOPMSG, OnSsbtnsetstopmsg)
	ON_BN_CLICKED(IDSSOK, OnCancelClk)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSingleServDlg message handlers


BOOL CSingleServDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	//We send the command back to the parent explicitly (rather than DDX)
	m_bDoSetStopMsg = false;
	m_bDoKill = false;
	m_bDoStop = false;
	m_bDoStart = false;

	//Set the window title to the name of the service
	this->SetWindowText(m_csShortName);

	//Set the button states to only allow valid actions
	SetButtonStates();
	
	return TRUE;  // return TRUE unless you set the focus to a control
}


void CSingleServDlg::SetButtonStates()
{
	//Set the button states based on the text in m_csState

	//Buttons are almost always disabled
	bool bStartButton = false;
	bool bStopButton = false;
	bool bSetStopMsgButton = false;
	bool bKillButton = true;
		
	//If it's not declared here, it's OFF
	//We only need to handle cases where a user can do something
	if (m_csState.Find("Running")!=-1)
	{
		bStopButton = true;
		bKillButton = true;
		bSetStopMsgButton = true;
	}
	else if (m_csState == " Stopped")
	{
		bStartButton = true;
		bKillButton = false;
	}

	//Set the buttons themselves
	CButton * cBTN = (CButton *) GetDlgItem(IDC_BTNSSSTART);
	cBTN->EnableWindow(bStartButton);
	cBTN = (CButton *) GetDlgItem(IDC_BTNSSSTOP);
	cBTN->EnableWindow(bStopButton);
	cBTN = (CButton *) GetDlgItem(IDC_SSBTNKILL);
	cBTN->EnableWindow(bKillButton);
	cBTN = (CButton *) GetDlgItem(IDC_SSBTNSETSTOPMSG);
	cBTN->EnableWindow(bSetStopMsgButton);

}

void CSingleServDlg::OnBtnssstart() 
{
	//Start Button
	//Set the command flag, and dismiss the dialog
	m_bDoStart = true;
	CDialog::OnOK(); 
}

void CSingleServDlg::OnBtnssstop() 
{
	//Stop Button
	//Set the command flag, and dismiss the dialog
	m_bDoStop = true;
	CDialog::OnOK(); 	
}

void CSingleServDlg::OnSsbtnkill() 
{
	//Kill Button
	//Set the command flag, and dismiss the dialog
	m_bDoKill = true;
	CDialog::OnOK(); 
}

void CSingleServDlg::OnSsbtnsetstopmsg() 
{
	//Set Stop Message Button
	//Set the command flag, and dismiss the dialog
	m_bDoSetStopMsg = true;
	CDialog::OnOK(); 
}

void CSingleServDlg::OnCancelClk() 
{
	//Cancel Button
	//We use the default cancel handler, because we check for IDOK in the parent
	CDialog::OnCancel();
}
